package top.drinyeit.nongmao.presenter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.Map;

import top.drinyeit.nongmao.activity.PersonalDataActivity;
import top.drinyeit.nongmao.app.Constant;
import top.drinyeit.nongmao.model.UserInfBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;
import top.drinyeit.nongmao.util.SPUtil;

/**
 * Created by bigHead on 2018/1/3.
 */

public class PersonalDataPresenter {
    private PersonalDataActivity personalDataView;

    public PersonalDataPresenter(PersonalDataActivity personalDataView){
        this.personalDataView = personalDataView;
    }

    //获取用户信息
    public void getInf(String user_id){
        if (SPUtil.getShareString(Constant.USER_ID).equals("")){
            return;
        }
        personalDataView.showLoading();
        CommonSubscriber<HttpResponse<UserInfBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<UserInfBean>>() {
            @Override
            public void onError(String e, int code) {
                personalDataView.dismissLoading();
                personalDataView.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<UserInfBean> o) {
                personalDataView.dismissLoading();
                if (o.getCode() == 1){
                    Glide.with(personalDataView)
                            .load(o.getData().getHead_photo_url())
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .dontTransform()
                            .into(personalDataView.head);
                    personalDataView.nick_name.setText(o.getData().getNick_name());

                    if (o.getData().getSex() == 1){
                        personalDataView.sex.setText("男");
                    }else {
                        personalDataView.sex.setText("女");
                    }
                }else {
                    personalDataView.showToastMsg(o.getMessage());
                }
                personalDataView.headPic = o.getData().getHead_photo_url();
                personalDataView.nickName = o.getData().getNick_name();
                personalDataView.sexNo = o.getData().getSex();
            }
        });
        RetrofitMethod.getInstance().getInf(subscriber,user_id);
    }

    //编辑用户信息
    public void editInf(Map<String,Object> map){
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        personalDataView.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                personalDataView.dismissLoading();
                personalDataView.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                personalDataView.dismissLoading();
                personalDataView.showToastMsg(o.getMessage());
            }
        });
        RetrofitMethod.getInstance().editInf(subscriber,map);
    }
}
